﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace DemoFSUtility
{
    public partial class FrmImageCompareHandler : Form
    {
        public FrmImageCompareHandler()
        {
            InitializeComponent();
        }

        private void btnCompare_Click(object sender, EventArgs e)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            #region 图像对比

            Bitmap bpLeft = this.pLeftImg.Image as Bitmap;
            Bitmap bpRight = this.pRightImg.Image as Bitmap;

            FSUtility.ImageCompareHandler imgCompareHandler = new FSUtility.ImageCompareHandler();
            List<Rectangle> diffRect = imgCompareHandler.CompareImage(bpLeft, bpRight, new Size(20, 20));

            if (diffRect == null
                || diffRect.Count == 0)
            {
                MessageBox.Show("未检测出不同");
                return;
            }

            Graphics gCave;
            //绘制在左图像
            using (gCave = Graphics.FromImage(bpLeft))
            {
                gCave.DrawRectangles(Pens.Red, diffRect.ToArray());
                gCave.Save();
            }
            //绘制在右图像
            using (gCave = Graphics.FromImage(bpRight))
            {
                gCave.DrawRectangles(Pens.Green, diffRect.ToArray());
                gCave.Save();
            }

            this.pLeftImg.Refresh();
            this.pRightImg.Refresh();
            #endregion

            sw.Stop();
            this.Text = sw.Elapsed.Milliseconds.ToString();
        }
    }
}
